<?php
   class upload{
   	private $size=2097152;
   	private $type=array("jpg","jpeg","gif","png");
   	private $path;
    private $error;
   	public function __construct($size='',$type='',$path=''){
   	     $this->size = empty($size)?$this->size:$size;
   	     $this->type = empty($type)?$this->type:$type;
   	     $this->path = empty($path)?$this->path:$path;
   	}
      public function show(){
          $arr = $this->toarr();
          $array=array();
         foreach ($arr as $key => $value) {
           if(!$this->tomake($value)){
               echo $this->error;
              continue;
           }else{
              $array[]=$this->movefile($value);
           }
         }
         return $array;
       }
       private function movefile($file){
         is_dir($this->path)?$this->path:mkdir($this->path,0777,true);
         $new = $this->path.'/'.time().mt_rand(1,1000).strrchr($file['name'],".");
         if(move_uploaded_file($file['tmp_name'],$new)){
          $file['new_name']=$new;
         }else{
             echo 11;die;
         }
         return $file;
       }
       private function tomake($file){
         if(!is_uploaded_file($file['tmp_name'])){
             $this->error = "上传文件异常";
              return false;
         }
         if($file['size']>$this->size){
            $this->error = "上传文件大小超过允许上产最大值";
            return false;
         }
         $fil =ltrim(strrchr($file['name'], '.'),'.');
         if(!in_array($fil,$this->type)){
            $this->error = "文件类型不正确";
            return false;
         }
         return true;   
       }
      private function toarr(){
         $arr = $_FILES;
         $array=array();
         $id=0; 
         foreach ($arr as $key => $value) {
            if(is_array($value['name'])){
               foreach ($value['name'] as $k => $v) {
                  $array[$id]['name']=$value['name'][$k];
                  $array[$id]['type']=$value['type'][$k];
                  $array[$id]['tmp_name']=$value['tmp_name'][$k];
                  $array[$id]['error']=$value['error'][$k];
                  $array[$id]['size']=$value['size'][$k];
                  $id++;                
               }
            }else{
                  $array[$id]=$value;
                  $id++;  
            }
         }
         return $array;
      }
   }

?>